//
//  BSTopicItem.m
//  Baisijie
//
//  Created by 王建伟 on 2017/3/21.
//  Copyright © 2017年 RangGuangyu. All rights reserved.
//

#import "BSTopicItem.h"
#import <MJExtension.h>

@implementation BSTopicItem

+ (NSDictionary *)mj_objectClassInArray {
    return @{@"top_comments" : [BSCommentItem class],
             @"vote" : [BSVotoItem class]};
}
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"topic_id" : @"id"};
}

- (CGFloat)cellHeight {
    if (_cellHeight == 0 || _showTextType != 0) {
        CGFloat cellWidth = KScreenW - 2 * BSMargin;
        NSDictionary *atts = @{NSFontAttributeName : [UIFont systemFontOfSize:17]};
        CGFloat textHeight = [_text boundingRectWithSize:CGSizeMake(cellWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:atts context:nil].size.height;
        CGFloat textOneLineHeight = [@"百思不得姐" sizeWithAttributes:atts].height;
        if (textHeight > textOneLineHeight * 7 && _showTextType == 0) {
            _showTextType = 2;
        }
        _cellHeight = textHeight + 1  + 50 + 40;
        if ([_type isEqualToString:@"text"]) {
            if (_showTextType == 1) {
                _cellHeight += 25;
            } else if (_showTextType == 2) {
                _cellHeight = textOneLineHeight * 7 + 1  + 50 + 40 + 25;
            }
        } else if ([_type isEqualToString:@"image"]) {
            CGFloat imageH = cellWidth * _image.height / _image.width;
            if (imageH > KScreenW) {
                imageH = KScreenW;
            }
            _cellHeight += imageH + BSMargin;
        } else if ([_type isEqualToString:@"gif"]) {
            CGFloat gifH = cellWidth * _gif.height / _gif.width;
            _cellHeight += gifH + BSMargin;
        } else if ([_type isEqualToString:@"video"]) {
            CGFloat videoH = cellWidth * _video.height / _video.width;
            if (videoH > cellWidth) {
                videoH = cellWidth;
            }
            _cellHeight += videoH + BSMargin;
        } else if ([_type isEqualToString:@"audio"]) {
            
        }
        if (_top_comments.count) {
            CGFloat commentH = BSMargin;
            for (BSCommentItem *item in _top_comments) {
                if ([item.cmt_type isEqualToString:@"audio"]) {
                    commentH += 30;
                } else {
                    NSString *commentStr = [NSString stringWithFormat:@"%@：%@",item.u.name,item.content];
                    commentH += [commentStr boundingRectWithSize:CGSizeMake(KScreenW - 40, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height + BSMargin * 0.5;
                }
            }
            _cellHeight += commentH + BSMargin;
        }
    }
    return _cellHeight;
}

@end
